package resources

import (
	"encoding/json"
	"errors"
	apis2 "gluenet/pkg/apis"
	"gluenet/pkg/gerrors"
	"gluenet/pkg/meta"
)

func Resrouce(ctx *apis2.GlueContext) {
	//var data = []struct {
	//	UpdateTime   string `json:"update_time" yaml:"update_time"`
	//	ResourceTree string `json:"resource_tree" yaml:"resource_tree"`
	//}{}

	var m *meta.Tree
	if err := ctx.DataBase.MapGet(&m); err != nil {
		if errors.Is(err, gerrors.DataNotFind) {
			ctx.WebContext.JSON(apis2.ResponseOK(nil))
		}
		n, _ := ctx.WebContext.JSON(apis2.ResponseReqErr(err, nil))
		ctx.Logger.Errorf("request response byte:%v config nodes err: %v", n, err)
		return
	}

	var tree = new(interface{})
	json.Unmarshal([]byte(m.ResourceTree), &tree)

	ctx.WebContext.JSON(apis2.ResponseOK(tree))
	//str := []byte(res)
	//var data = new(interface{})
	//err := json.Unmarshal(str, data)
	//if err != nil {
	//	n, _ := ctx.WebContext.JSON(apis.ResponseReqErr(err, nil))
	//	ctx.Logger.Errorf("request response byte:%v config nodes err: %v", n, err)
	//	return
	//}
	//ctx.WebContext.JSON(apis.ResponseOK(data))
}

const res = `
{
	"name": "glue",
	"type": "zone",
	"guid": "0",
	"update_time": "2022-04-20T15:10:54.753010445Z",
	"state": "alive",
	"info": {},
	"child_alive": 1,
	"child_size": 1,
	"child_data": [
		{
			"name": "beijing",
			"type": "zone",
			"guid": "0",
			"update_time": "2022-04-20T15:10:54.753010445Z",
			"state": "alive",
			"info": {},
			"child_alive": 2,
			"child_size": 2,
			"child_data": [
				{
					"type": "host",
					"name": "node38",
					"guid": "aaaa-bbbb-cccc-dddd",
					"update_time": "2022-04-20T15:10:54.753010445Z",
					"state": "alive",
					"info": {},
					"child_alive": 1,
					"child_size": 1,
					"child_data": [
						{
							"name": "my-web-server",
							"type": "application",
							"guid": "bbbb-aaaa-dddd-cccc",
							"update_time": "2022-04-20T15:10:54.753010445Z",
							"state": "alive",
							"info": {},
							"child_alive": 2,
							"child_size": 2,
							"child_data": [
								{
									"name": "impl",
									"type": "container",
									"guid": "-1",
									"update_time": "2022-04-20T15:10:54.753010445Z",
									"state": "alive",
									"info": {
										"Image": "mysql",
										"ContainerId": "fadsf312312312312332131231231231231231"
									},
									"child_alive": 0,
									"child_size": 0,
									"child_data": []
								},
								{
									"name": "web",
									"type": "container",
									"guid": "-1",
									"update_time": "2022-04-20T15:10:54.753010445Z",
									"state": "alive",
									"info": {
										"Image": "nginx",
										"ContainerId": "31231231231233213123123123123123123123"
									},
									"child_alive": 0,
									"child_size": 0,
									"child_data": []
								}
							]
						}
					]
				},
				{
					"type": "host",
					"name": "node39",
					"guid": "6yfd-fsdf-dfsf-342g",
					"update_time": "2022-04-20T15:10:54.753010445Z",
					"state": "alive",
					"info": {},
					"child_alive": 1,
					"child_size": 1,
					"child_data": [
						{
							"name": "my-web-server",
							"type": "application",
							"guid": "dfwe-ifke-dfwe-cccc",
							"update_time": "2022-04-20T15:10:54.753010445Z",
							"state": "alive",
							"info": {},
							"child_alive": 1,
							"child_size": 1,
							"child_data": [
								{
									"InstancesType": "container",
									"InstancesName": "app-server",
									"name": "app-server",
									"type": "container",
									"guid": "-1",
									"update_time": "2022-04-20T15:10:54.753010445Z",
									"state": "alive",
									"info": {
										"Image": "my-app",
										"ContainerId": "pokje231231233213123123ijhg23123123123"
									},
									"child_alive": 0,
									"child_size": 0,
									"child_data": []
								}
							]
						}
					]
				},
				{
					"name": "k8s-master",
					"type": "k8scluster",
					"guid": "dfas-239d-sfas-4sdf",
					"update_time": "2022-04-20T15:10:54.753010445Z",
					"state": "alive",
					"info": {
					},
					"child_alive": 1,
					"child_size": 1,
					"child_data": [
						{
							"name": "my-web-server",
							"type": "application",
							"guid": "fsdf-5g67-5ydf-6yhg",
							"update_time": "2022-04-20T15:10:54.753010445Z",
							"state": "alive",
							"info": {},
							"child_alive": 2,
							"child_size": 2,
							"child_data": [
								{
									"name": "server",
									"type": "pod",
									"guid": "-1",
									"update_time": "2022-04-20T15:10:54.753010445Z",
									"state": "alive",
									"info": {
										"PodInfos": [
											{
												"Image": "mysql",
												"ContainerId": "fadsf312312312312332131231231231231231"
											}
										],
										"NameSpace": "default",
										"Ip": "10.30.10.11",
										"Node": "k8s-master",
										"Uid": "a59bf531-09ds-4a02-48ds-fc4fe9je782b",
										"Ports": [
											{
												"containerPort": 4171,
												"protocol": "TCP"
											},
											{
												"containerPort": 4161,
												"protocol": "TCP"
											},
											{
												"containerPort": 4150,
												"protocol": "TCP"
											}
										],
										"Yaml": "{\"apiVersion\":\"v1\",\"kind\":\"Pod\",\"metadata\":{\"annotations\":{},\"labels\":{\"app\":\"glue-apis\"},\"name\":\"glue-apis\",\"namespace\":\"default\"},\"spec\":{\"containers\":[{\"command\":[\"/go/main\"],\"env\":[{\"name\":\"MYSQL_IP\",\"value\":\"$(GLUE_DATA_MYSQL_SERVICE_SERVICE_HOST)\"},{\"name\":\"MYSQL_PORT\",\"value\":\"$(GLUE_DATA_MYSQL_SERVICE_SERVICE_PORT)\"},{\"name\":\"INFLUXDB_URL\",\"value\":\"http://$(GLUE_DATA_INFLUXDB_SERVICE_SERVICE_HOST):$(GLUE_DATA_INFLUXDB_SERVICE_SERVICE_PORT)\""
									},
									"child_alive": 0,
									"child_size": 0,
									"child_data": []
								}
							]
						}
					]
				},
				{
					"name": "k8s-worker01",
					"type": "k8scluster",
					"guid": "ddd2-239d-sfas-4sdf",
					"update_time": "2022-04-20T15:10:54.753010445Z",
					"state": "alive",
					"info": {
					},
					"child_alive": 1,
					"child_size": 1,
					"child_data": [
						{
							"name": "my-web-server",
							"type": "application",
							"guid": "fsdf-5g67-5ydf-6yhg",
							"update_time": "2022-04-20T15:10:54.753010445Z",
							"state": "alive",
							"info": {},
							"child_alive": 1,
							"child_size": 1,
							"child_data": [
								{
									"name": "impl",
									"type": "pod",
									"guid": "-1",
									"update_time": "2022-04-20T15:10:54.753010445Z",
									"state": "alive",
									"info": {
										"PodInfos": [
											{
												"Image": "mysql",
												"ContainerId": "fadsf312312312312332131231231231231231"
											}
										],
										"NameSpace": "default",
										"Ip": "10.30.10.11",
										"Node": "k8s-worker01",
										"Uid": "a59bf531-462f-4a02-a851-fc4fefee782b",
										"Ports": [
											{
												"containerPort": 4171,
												"protocol": "TCP"
											},
											{
												"containerPort": 4161,
												"protocol": "TCP"
											},
											{
												"containerPort": 4150,
												"protocol": "TCP"
											}
										],
										"Yaml": "{\"apiVersion\":\"v1\",\"kind\":\"Pod\",\"metadata\":{\"annotations\":{},\"labels\":{\"app\":\"glue-apis\"},\"name\":\"glue-apis\",\"namespace\":\"default\"},\"spec\":{\"containers\":[{\"command\":[\"/go/main\"],\"env\":[{\"name\":\"MYSQL_IP\",\"value\":\"$(GLUE_DATA_MYSQL_SERVICE_SERVICE_HOST)\"},{\"name\":\"MYSQL_PORT\",\"value\":\"$(GLUE_DATA_MYSQL_SERVICE_SERVICE_PORT)\"},{\"name\":\"INFLUXDB_URL\",\"value\":\"http://$(GLUE_DATA_INFLUXDB_SERVICE_SERVICE_HOST):$(GLUE_DATA_INFLUXDB_SERVICE_SERVICE_PORT)\""
									},
									"child_alive": 0,
									"child_size": 0,
									"child_data": []
								},
								{
									"name": "web",
									"type": "pod",
									"guid": "-1",
									"update_time": "2022-04-20T15:10:54.753010445Z",
									"state": "alive",
									"info": {
										"PodInfos": [
											{
												"Image": "mysql",
												"ContainerId": "fadsf31231231lf9473213123123123194jub"
											}
										],
										"NameSpace": "default",
										"Ip": "10.30.10.14",
										"Node": "k8s-worker01",
										"Uid": "a59okjh6-462f-987n-a851-fc4fefee782b",
										"Ports": [
											{
												"containerPort": 4171,
												"protocol": "TCP"
											},
											{
												"containerPort": 4161,
												"protocol": "TCP"
											},
											{
												"containerPort": 4150,
												"protocol": "TCP"
											}
										],
										"Yaml": "{\"apiVersion\":\"v1\",\"kind\":\"Pod\",\"metadata\":{\"annotations\":{},\"labels\":{\"app\":\"glue-apis\"},\"name\":\"glue-apis\",\"namespace\":\"default\"},\"spec\":{\"containers\":[{\"command\":[\"/go/main\"],\"env\":[{\"name\":\"MYSQL_IP\",\"value\":\"$(GLUE_DATA_MYSQL_SERVICE_SERVICE_HOST)\"},{\"name\":\"MYSQL_PORT\",\"value\":\"$(GLUE_DATA_MYSQL_SERVICE_SERVICE_PORT)\"},{\"name\":\"INFLUXDB_URL\",\"value\":\"http://$(GLUE_DATA_INFLUXDB_SERVICE_SERVICE_HOST):$(GLUE_DATA_INFLUXDB_SERVICE_SERVICE_PORT)\""
									},
									"child_alive": 0,
									"child_size": 0,
									"child_data": []
								}
							]
						}
					]
				}
			
			]
		}
	]
}
`
